//
//  NewTaskDetailViewController.m
//  rrqs
//
//  Created by mac book on 2019/5/5.
//

#import "NewTaskDetailViewController.h"
#import "FinishOrderDetailTableViewCell.h"
#import "FinishOrderDetailInfoTableViewCell.h"
#import "FinishOrderDetailTopTableViewCell.h"



#define Cell_H  90.f/HEIGHT_6S_SCALE


@interface NewTaskDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listDataArray; // 外卖数据源


@property(nonatomic,strong) UIView* earningsView;

@property (nonatomic,strong)UIButton *gukeBtn;
@property (nonatomic,strong)UIButton *shangjiaBtn;




@end

@implementation NewTaskDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self creatsubViews];
    
    
    [self queryData];
}



#pragma mark -  DataSource
-(void)queryData{
    
    
    NSDictionary* arg = @{
                          @"ince":@"get_order_infos",
                          @"emp_id":[UserModel sharedInstanced].userId,
                          @"main_id":_waiMaiDodelListitems.order_id
                          };
    
    [DBNet PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
        NSLog(@"详情data = %@",data);
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* emptyDict = [data objectForKey:@"order"];
            
            self.listDataArray = [NSMutableArray arrayWithArray:emptyDict[@"goods_info"]];

            
            [self.tableView reloadData];
            
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
        
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
    
    
}



-(void)creatsubViews{
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame),  CGRectGetHeight(self.view.frame) - NAVGATIONBAR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.showsVerticalScrollIndicator = NO;
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [self creatFooterView];

    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    if (@available(iOS 11.0, *))
    {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
    }
    
    [self.tableView registerClass:[FinishOrderDetailTopTableViewCell class] forCellReuseIdentifier:@"NewTaskDetailTopTableViewCell"];

    [self.tableView registerClass:[FinishOrderDetailTableViewCell class] forCellReuseIdentifier:@"NewTaskDetailTableViewCell"];
    [self.tableView registerClass:[FinishOrderDetailInfoTableViewCell class] forCellReuseIdentifier:@"NewTaskDetailInfoTableViewCell"];
  
    
}


#pragma mark 提交按钮
-(UIView *)creatFooterView{
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    
    self.gukeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.gukeBtn addTarget:self action:@selector(gukeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.gukeBtn.layer.masksToBounds = YES;
    self.gukeBtn.layer.cornerRadius = 5;
    self.gukeBtn.titleLabel.font= kFontNameSize(16);
    [self.gukeBtn setTitle:@"联系顾客" forState:UIControlStateNormal];
    self.gukeBtn.backgroundColor=  kMyColor(248, 159, 39);
    [self.gukeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerView addSubview:self.gukeBtn];

    
    
    self.shangjiaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shangjiaBtn addTarget:self action:@selector(shangjiaBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.shangjiaBtn.layer.masksToBounds = YES;
    self.shangjiaBtn.layer.cornerRadius = 5;
    self.shangjiaBtn.titleLabel.font= kFontNameSize(16);
    [self.shangjiaBtn setTitle:@"联系商家" forState:UIControlStateNormal];
    self.shangjiaBtn.backgroundColor=  kMyColor(248, 159, 39);
    [self.shangjiaBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerView addSubview:self.shangjiaBtn];
    
    
    NSArray *masonryViewArray = @[self.gukeBtn,self.shangjiaBtn];
    
    [masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:20 tailSpacing:20];
    
    // 设置array的垂直方向的约束
    [masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(footerView).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    
    
    
    
    return footerView;
}

-(void)gukeBtnAction{
    
}

-(void)shangjiaBtnAction{
    
}


#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 1) {
        return self.listDataArray.count;
        
    }else{
        return 1;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 90;
        
    }else if (indexPath.section == 1){
        
        return Cell_H;
        
    }else{
        
        return 140;

    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        FinishOrderDetailTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewTaskDetailTopTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return cell;
        
    }else if (indexPath.section == 1) {
        
        FinishOrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewTaskDetailTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
//        cell.dataDic = self.listDataArray[indexPath.row];
        
        return cell;
        
    }else{
        
        
        FinishOrderDetailInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewTaskDetailInfoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
        
        return cell;
        
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 1) {
        return 40;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.001;
    }else{
        return 40;
    }
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section != 0) {

        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = [UIColor whiteColor];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, 40)];
        [headerView addSubview:title];
        title.font = kFontNameSize(15);
        
        if (section == 0) {

            title.text = @"商品列表";

        }else{
            title.text = @"订单信息";

        }
        return headerView;

        
    }
        
    return nil;

    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *footer = [[UIView alloc] init];
    footer.backgroundColor = [UIColor clearColor];
    
    if (section == 1) {
        
        UIView *back = [[UIView alloc] init];
        back.backgroundColor = [UIColor whiteColor];
        [footer addSubview:back];
        [back mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(footer);
            make.bottom.equalTo(footer).offset(-10);
        }];
        

        
        UILabel *title = [[UILabel alloc] init];
        title.font = kFontNameSize(14);
        title.text = @"备注：无";
        [back addSubview:title];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.bottom.equalTo(back);
            make.left.equalTo(back).offset(10);
            make.width.mas_equalTo(80);
            
        }];
        
        UILabel *content = [[UILabel alloc] init];
        content.textColor = [UIColor darkGrayColor];
        content.font = kFontNameSize(14);
        content.numberOfLines = 0;
        content.lineBreakMode = NSLineBreakByCharWrapping;
        [back addSubview:content];
        [content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(back);
            make.left.equalTo(title.mas_right);
            make.right.equalTo(back);
            
        }];
        content.userInteractionEnabled=YES;
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
        [content addGestureRecognizer:labelTapGestureRecognizer];
        
        
//        content.text = self.twoModel.site_addr;
        
    }
    
    
    return footer;
    
}

-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
//    [CommonUntils naviAddressWithDestination:self.twoModel.site_addr withLatitude:self.twoModel.latitude withLongitude:self.twoModel.longitude withController:self];
}

/**
 *  拨打电话
 *
 *  @param phoneNumber 号码
 */
- (void)callPhone:(NSString *)phoneNumber{
    
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNumber];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    
}


//确认送达
-(void)makeSureSendData{
    
    [self showHUD];
    
    NSDictionary* arg = @{@"ince":@"qs_send_food",
                          @"emp_id":[UserModel sharedInstanced].userId,
//                          @"order_id":self.twoModel.order_id
                          
                          };
    
    [DBNet PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
        NSLog(@"data = %@",data);
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSString *sucess = data[@"sucess"];
            [self hidHUD: sucess success:YES complete:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
            
        }else{
            [self hidHUD:[data objectForKey:@"fail"]];
        }
        
    } failure:^(NSError *error) {
        [self hidHUD:@"网络异常"];
    }];
    
}

//确认取货
- (void)makeSureGetData {
    
    [self showHUD];
    NSDictionary* arg = @{
                          @"ince":@"qs_get_food",
//                          @"order_id":self.twoModel.order_id,
                          @"emp_id":[UserModel sharedInstanced].userId,

                          };
    [DBNet PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
        NSLog(@"data = %@",data);
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSString *sucess = data[@"sucess"];
            [self hidHUD: sucess success:YES complete:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
            
        }else{
            [self hidHUD:[data objectForKey:@"fail"]];
        }
        
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
}




#pragma mark -  property package
- (NSMutableArray *)listDataArray
{
    if (!_listDataArray) {
        _listDataArray = @[].mutableCopy;
    }
    return _listDataArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
